package com.whfc.fse.config;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.spring.AppContextUtil;
import com.whfc.common.third.ansafe.AnSafeApi;
import com.whfc.common.third.ansafe.AnSafeApiProperties;
import com.whfc.common.third.yuke.YukeApi;
import com.whfc.common.third.yuke.YukeApiProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @Description:
 * @author: sun_guodong
 * @version: 1.0
 * @date: 2019/10/10 10:13
 */
@Configuration
@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "com.whfc.fse.properties", autoRefreshed = true),
        @NacosPropertySource(dataId = "com.whfc.common.properties", autoRefreshed = true)
})
public class FseConfig {

    @Value("${spring.profiles.active}")
    private String profile;

    @Bean
    public AppContextUtil appContextUtil() {
        return new AppContextUtil();
    }

    @Bean("msFilePathConfig")
    public FilePathConfig msFilePathConfig() {
        FilePathConfig config = new FilePathConfig("ms", profile);
        return config;
    }

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setThreadGroupName("fse-thg");
        taskExecutor.setThreadNamePrefix("fse-th");
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(1024);
        taskExecutor.setKeepAliveSeconds(30000);
        return taskExecutor;
    }

    /**
     * 安拾平台 特种设备api参数
     *
     * @return
     */
    @Bean("ansafeApiProperties")
    @ConfigurationProperties("ansafe")
    public AnSafeApiProperties anSafeApiProperties() {
        return new AnSafeApiProperties();
    }

    /**
     * 安拾平台 特种设备api
     *
     * @param anSafeApiProperties
     * @return
     */
    @Bean
    public AnSafeApi anSafeApi(@Qualifier("ansafeApiProperties") AnSafeApiProperties anSafeApiProperties) {
        AnSafeApi api = new AnSafeApi(anSafeApiProperties.getAccessKey());
        return api;
    }

    /**
     * 合肥宇科 特种设备api参数
     *
     * @return
     */
    @Bean("yukeApiProperties")
    @ConfigurationProperties("yuke")
    public YukeApiProperties yukeApiProperties() {
        return new YukeApiProperties();
    }

    /**
     * 合肥宇科 特种设备api
     *
     * @param yukeApiProperties
     * @return
     */
    @Bean
    public YukeApi yukeApi(@Qualifier("yukeApiProperties") YukeApiProperties yukeApiProperties) {
        YukeApi yukeApi = new YukeApi(yukeApiProperties.getUsername(), yukeApiProperties.getPassword());
        return yukeApi;
    }
}
